package com.vic.framework.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 请求参数对象
 * @author VIC
 *
 */
public class Param {

	/*private Map<String, Object> paramMap;

	
	
	public Param(Map<String, Object> paramMap) {
		super();
		this.paramMap = paramMap;
	}

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}
	
	public  boolean isEmpty(){
		return MapUtils.isEmpty(paramMap);
	}
*/
	
	private List<FormParam> formParamList;
	
	private List<FileParam> fileParamList;

	
	
	
	public Param(List<FormParam> formParamList) {
		super();
		this.formParamList = formParamList;
	}

	public Param(List<FormParam> formParamList, List<FileParam> fileParamList) {
		super();
		this.formParamList = formParamList;
		this.fileParamList = fileParamList;
	}

	public List<FormParam> getFormParamList() {
		return formParamList;
	}

	public void setFormParamList(List<FormParam> formParamList) {
		this.formParamList = formParamList;
	}

	public List<FileParam> getFileParamList() {
		return fileParamList;
	}

	public void setFileParamList(List<FileParam> fileParamList) {
		this.fileParamList = fileParamList;
	}
	
	
	/**
	 * 获取请求参数映射
	 */
	public Map<String, Object> getFieldMap(){
		Map<String, Object> fieldMap = new HashMap<String, Object>();
		if(!CollectionUtils.isEmpty(formParamList)) {
			for(FormParam formParam : formParamList) {
				String fieldName = formParam.getFieldName();
				Object fieldValue = formParam.getFieldValue();
				if(fieldMap.containsKey(fieldName)) {
					fieldValue = fieldMap.get(fieldName) + "," + fieldValue;
				}
				fieldMap.put(fieldName, fieldValue);
			}
		}
		return fieldMap;
	}
	
	/**
	 * 获取上传文件映射
	 */
	
	public Map<String, List<FileParam>> getFileMap(){
		Map<String, List<FileParam>> fileMap = new HashMap<String, List<FileParam>>();
		if(!CollectionUtils.isEmpty(fileParamList)) {
			for(FileParam fileParam : fileParamList) {
				String fieldName = fileParam.getFieldName();
				List<FileParam> fileParams;
				if(fileMap.containsKey(fieldName)) {
					fileParams = fileMap.get(fieldName);
				}else {
					fileParams = new ArrayList<FileParam>();
				}
				fileParams.add(fileParam);
				fileMap.put(fieldName, fileParams);
			}
		}
		
		return fileMap;
		
	}
	
	/**
	 * 获得所有上传文件
	 */
	public List<FileParam> getFileList(String fieldName){
		return getFileMap().get(fieldName);
	}
	
	/**
	 * 获得唯一上传文件
	 */
	public FileParam getFile(String fieldName){
		List<FileParam> list = getFileList(fieldName);
		if(CollectionUtils.isNotEmpty(list) && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 验证参数是否为空
	 */
	public boolean isEmpty(){
		return CollectionUtils.isEmpty(formParamList) && CollectionUtils.isEmpty(fileParamList);
	}
	
	/**
	 * 根据参数名获取String类型参数值
	 */
	public String getString(String name) {
		return getFieldMap().get(name) + "";
	}
	
	/**
	 * 根据参数名获得double类型
	 */
	public double getDouble(String name) {
		return Double.parseDouble(getFieldMap().get(name) + "");
	}
	/**
	 * 根据参数名获得long类型
	 */
	public long getLong(String name) {
		return Long.parseLong(getFieldMap().get(name) + "");
	}
	
	/**
	 * 根据参数名获得int类型
	 */
	public int getInt(String name) {
		return Integer.parseInt(getFieldMap().get(name) + "");
	}
	
	/**
	 * 根据参数名获得boolean类型
	 */
	public boolean getBooelan(String name) {
		return Boolean.parseBoolean(getFieldMap().get(name) + "");
	}
	
	public static void main(String[] args) {
		System.out.println("+++" + String.valueOf((char)29)+"+++" );
		System.out.println("--");
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		List<FileParam>  files = this.fileParamList;
		List<FormParam> forms = this.formParamList;
		if(files != null) {
			sb.append("fileParamList:\n");
			for(FileParam f : files) {
				sb.append(f.toString()).append("\n");
			}
		}
		if(forms != null) {
			sb.append("formParamList:\n");
			for(FormParam f : forms) {
				sb.append(f.toString()).append("\n");
			}
		}
		return sb.toString();
	}
	
	
	
	
}
